package com.sservice.web.controller.admin;

import com.sservice.base.SameUrlData;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.common.CompareObject;
import com.sservice.service.service.IComService;
import com.sservice.service.service.IDepService;
import com.sservice.service.service.ISysLogService;
import com.sservice.service.service.IUserService;
import com.sservice.urp.entity.Com;
import com.sservice.urp.vo.ComVo;
import com.sservice.urp.vo.DepVo;
import com.sservice.urp.vo.UserVo;
import com.sservice.web.log.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/com")
public class ComController {
//    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IComService comService;
    @Autowired
    private ISysLogService sysLogService;
    @Autowired
    private IDepService depService;
    @Autowired
    private IUserService userService;


    /**
     * 创建
     *
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Object> create(Com arg, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Com com = comService.selectByComName(arg.getName().trim());
        if (com != null) {
            return new BaseResult(false, "单位名称已存在!", 403);
        }
        int service_res = comService.insertSelective(arg);
        if (service_res > 0) {
            sysLogService.log("创建", request, Integer.valueOf(arg.getId()), Action.comType);
        }
        BaseResult res = new BaseResult(true, service_res);
        return res;
    }

    /***
     *  单位详情
     * @param arg
     * @param request
     * @return
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<Object> get(Com arg, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Com com = comService.selectByPrimaryKey(arg.getId());
        return new BaseResult(true, com);
    }

    /**
     * @param arg
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Object> delete(Com arg, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        DepVo dv = new DepVo();
        UserVo uv = new UserVo();
        BaseResult<Object> res;
        dv.setComid(String.valueOf(arg.getId()));
        uv.setComid(arg.getId());
        if (depService.queryCountByComId(dv) > 0) {
            res = new BaseResult<Object>(false, "该公司关联部门，不能删除!", 403);
        } else if (userService.queryCountByComId(uv) > 0) {
            res = new BaseResult<Object>(false, "该公司关联用户，不能删除!", 403);
        } else {
            int service_res = comService.deleteByPrimaryKey(arg.getId());
            if (service_res > 0) {
                sysLogService.log("删除", request, Integer.valueOf(arg.getId()), Action.comType);
            }
            res = new BaseResult<Object>(true, service_res);
        }
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(Com arg, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Com com = comService.selectByPrimaryKey(arg.getId());
        int service_res = comService.updateByPrimaryKeySelective(arg);
        if (service_res > 0) {

            Map<String, Object> res = CompareObject.contrastObj(arg, com);
            if (res.get("signature") != null) {
                sysLogService.log("更改公章：" + res.get("signature"), request, Integer.valueOf(arg.getId()), Action.comType);
            }
            if (res.get("signaturePaper") != null) {
                sysLogService.log("更改合同章：" + res.get("signaturePaper"), request, Integer.valueOf(arg.getId()), Action.comType);
            }
            if (res.get("signatureFinance") != null) {
                sysLogService.log("更改财务章：" + res.get("signatureFinance"), request, Integer.valueOf(arg.getId()), Action.comType);
            }
            sysLogService.log(res, request, Integer.valueOf(arg.getId()), Action.comType);
        }
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }


    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(ComVo arg) {
        if (arg.getSysId() == null)
            arg.setSysId(ComVo.DEFAULT_COM);
        List<Com> list = comService.queryByPage(arg);
        int total = comService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
